﻿using IsmsExplorer.Model.Projects;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using IsmsExplorer.Infrastructure;
using IsmsExplorer.Infrastructure.RepositoryFramework;

namespace IsmsExplorer.UnitTests {
    /// <summary>
    ///This is a test class for IProjectRepositoryTest and is intended
    ///to contain all IProjectRepositoryTest Unit Tests
    ///</summary>
    [TestClass()]
    public class IProjectRepositoryTest {
        private TestContext _testContextInstance;
        private IProjectRepository _repository;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext {
            get {
                return _testContextInstance;
            }
            set {
                _testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}

        //
        //Use TestInitialize to run code before running each test
        [TestInitialize()]
        public void MyTestInitialize() {
            _repository = RepositoryFactory.GetRepository<IProjectRepository, Project>(new UnitOfWork());
        }

        //Use TestCleanup to run code after each test has run
        [TestCleanup()]
        public void MyTestCleanup() {
            _repository = null;
        }

        #endregion

        /// <summary>
        /// Test for creating, deleting, updating and finding Projects.
        ///</summary>
        [TestMethod()]
        public void IRepositoryCrudTest() {
            string name = "My Test Project";
            Project actual = _repository.FindBy(name);
            // Initially there is no Project, hence this must be null!
            Assert.IsNull(actual, "Project returned is NOT null");
            // Now create a new Project and insert in database.
            DateTime? today = new DateTime(2013, 1, 15, 17, 42, 18);
            Project newProject = new Project(name) {
                StartDate = null //today
            };
            // Initially a new item had null ID.
            Assert.AreEqual(0, newProject.ID);
            _repository.Add(newProject);
            _repository.UnitOfWork.Commit();
            // Once committed to the data store, the new item ID is set (non-null)!
            Assert.AreNotEqual(0, newProject.ID);

            actual = _repository.FindBy(newProject.ID);
            Assert.IsNotNull(actual, "Project returned IS null");
            Assert.AreEqual(name, actual.Name);

            IList<Project> projects = _repository.FindAll();
            Assert.AreEqual(1, projects.Count);

            // Update the Project start date.
            actual.StartDate = today;
            _repository[actual.ID] = actual;
            _repository.UnitOfWork.Commit();
            actual = _repository.FindBy(newProject.ID);
            Assert.IsNotNull(actual, "Project returned IS null");
            Assert.AreEqual(today, actual.StartDate);

            // Now remove the Project.
            _repository.Remove(actual);
            _repository.UnitOfWork.Commit();

            projects = _repository.FindAll();
            Assert.AreEqual(0, projects.Count);

        } // end IRepositoryCrudTest()
    }
}
